home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / ARPCMD.C < prev    next >
Text File  |  1993-09-19  |  4KB  |  196 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include "global.h"
  4. #include "mbuf.h"
  5. #include "timer.h"
  6. #include "enet.h"
  7. #include "ax25.h"
  8. #include "arp.h"
  9. #include "netuser.h"
  10. #include "cmdparse.h"
  11. #include "commands.h"
  12.  
  13. char *Arptypes[] = {
  14.         "NET/ROM",
  15.         "10 Mb Ethernet",
  16.         "3 Mb Ethernet",
  17.         "AX.25",
  18.         "Pronet",
  19.         "Chaos",
  20.         "",
  21.         "Arcnet",
  22. };
  23.  
  24. static int near
  25. get_hwtype(char *cp)
  26. {
  27.     /* This is a kludge. It really ought to be table driven */
  28.     switch(*cp) {
  29.     case 'n':    /* Net/Rom pseudo-type */
  30.         return ARP_NETROM;
  31.     case 'e':    /* "ether" */
  32.         return ARP_ETHER;
  33.     case 'a':    /* "ax25" */
  34.         switch(cp[1]) {
  35.         case 'x':
  36.             return ARP_AX25;
  37.         case 'r':
  38.             return ARP_ARCNET;
  39.         }
  40.     }
  41.     tprintf("Unknown hardware type %s\n",cp);
  42.     return -1;
  43. }
  44.  
  45. static int
  46. doarpadd(int argc,char **argv,void *p)
  47. {
  48.     int32 addr;
  49.     struct arp_tab *ap;
  50.     char hwaddr[AXALEN];
  51.  
  52.     int pub = (*argv[0] == 'p');               /* Is this entry published? */
  53.     int hardware = get_hwtype(argv[2]);
  54.     struct arp_type *at = &Arp_type[hardware];
  55.  
  56.     if((addr = resolve(argv[1])) == 0){
  57.         tprintf(Badhost,argv[1]);
  58.         return 1;
  59.     }
  60.     if(hardware == -1) {
  61.         return -1;
  62.     }
  63.     if(at->scan == NULLFP){
  64.         tputs("Attach device first\n");
  65.         return -1;
  66.     }
  67.     /* If an entry already exists, clear it */
  68.     if((ap = arp_lookup(hardware,addr)) != NULLARP) {
  69.         arp_drop(ap);
  70.     }
  71.     /* Destination address */
  72.     (*at->scan)(hwaddr,argv[3]);
  73.  
  74.     ap = arp_add(addr,hardware,hwaddr,pub,0);      /* Put in table */
  75.     stop_timer(&ap->timer);                     /* Make entry permanent */
  76.     set_timer(&ap->timer,0L);
  77.     return 0;
  78. }
  79.  
  80. /* Remove an ARP entry */
  81. static int
  82. doarpdrop(int argc,char **argv,void *p)
  83. {
  84.     int32 addr;
  85.     struct arp_tab *ap;
  86.  
  87.     if((addr = resolve(argv[1])) == 0){
  88.         tprintf(Badhost,argv[1]);
  89.         return 1;
  90.     }
  91.     if((ap = arp_lookup(get_hwtype(argv[2]),addr)) != NULLARP) {
  92.         arp_drop(ap);
  93.     }
  94.     return 0;
  95. }
  96.  
  97. /* Flush all automatic entries in the arp cache */
  98. static int
  99. doarpflush(int argc,char **argv,void *p)
  100. {
  101.     struct arp_tab *ap;
  102.  
  103.     for(ap = Arp_tab; ap != NULLARP; ap = ap->next) {
  104.         if(dur_timer(&ap->timer) != 0) {
  105.             arp_drop(ap);
  106.         }
  107.     }
  108.     return 0;
  109. }
  110.  
  111. /* Dump ARP table */
  112. static void near
  113. dumparp(void)
  114. {
  115.     struct arp_tab *ap;
  116.  
  117.     tprintf("received %u badtype %u bogus addr %u reqst in %u replies %u reqst out %u\n" \
  118.             "IP addr          Type           Time Q Addr\n",
  119.         Arp_stat.recv,
  120.         Arp_stat.badtype,
  121.         Arp_stat.badaddr,
  122.         Arp_stat.inreq,
  123.         Arp_stat.replies,
  124.         Arp_stat.outreq);
  125.  
  126.     for(ap = Arp_tab; ap != NULLARP; ap = ap->next) {
  127.         tprintf("%-17s",inet_ntoa(ap->ip_addr));
  128.         if(ap->hardware == ARP_AX25) {
  129.             tputs("AX.25 [");
  130.             switch(ap->flags) {
  131.             case DATAGRAM_MODE:
  132.                 tputs("data] ");
  133.                 break;
  134.             case CONNECT_MODE:
  135.                 tputs("vc]   ");
  136.                 break;
  137.             case IPCAM_MODE:
  138.                 tputs("ipcam]");
  139.                 break;
  140.             default:
  141.                 tputs("def]  ");
  142.                 break;
  143.             }
  144.         } else {
  145.             tprintf("%-13.13s",Arptypes[ap->hardware]);
  146.         }
  147.         tprintf("  %-5ld",read_timer(&ap->timer)/1000L);
  148.  
  149.         if(ap->state == ARP_PENDING) {
  150.             tprintf("%-2u",len_q(ap->pending));
  151.         } else {
  152.             tputs("  ");
  153.         }
  154.         if(ap->state == ARP_VALID) {
  155.             if(Arp_type[ap->hardware].format != NULL) {
  156.                 char e[MAXPATH];
  157.                 (*Arp_type[ap->hardware].format)(e,ap->hw_addr);
  158.                 tputs(e);
  159.             }
  160.         } else {
  161.             tputs("[unknown]");
  162.         }
  163.         if(ap->pub) {
  164.             tputs(" (published)");
  165.         }
  166.         tputs("\n");
  167.     }
  168.     return;
  169. }
  170.  
  171. int
  172. doarp(int argc,char **argv,void *p)
  173. {
  174.     struct cmds Arpcmds[] = {
  175.         "add", doarpadd, 0, 4,
  176.         "arp add <hostid> ether|ax25|netrom <ether addr|callsign>",
  177.  
  178.         "drop", doarpdrop, 0, 3,
  179.         "arp drop <hostid> ether|ax25|netrom",
  180.  
  181.         "flush", doarpflush, 0, 0,
  182.         NULLCHAR,
  183.  
  184.         "publish", doarpadd, 0, 4,
  185.         "arp publish <hostid> ether|ax25|netrom <ether addr|callsign>",
  186.  
  187.         NULLCHAR,
  188.     };
  189.  
  190.     if(argc < 2) {
  191.         dumparp();
  192.         return 0;
  193.     }
  194.     return subcmd(Arpcmds,argc,argv,p);
  195. }
  196.